package com.fernfx.xingtan.utils;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseApplication;


/**
 * ToastUtil
 */
public class ToastUtil {

    private static Handler handler = null;

    private static int intY = 0;
    private static Toast toast = null;

    /**
     * 提示信息 黑色背景 下方显示
     *
     * @param msg
     */
    public static void showBottomtoast(final String msg) {
        showBottomtoast(msg, Toast.LENGTH_SHORT);
    }

    public static void showBottomtoast(final String msg, final int duration) {
        if (!TextUtils.isEmpty(msg)) {
            if (handler == null) {
                handler = new Handler(Looper.getMainLooper());
            }
            handler.post(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    if (toast == null) {
                        toast = Toast.makeText(BaseApplication.getInstance(), msg, duration);
                        intY = BaseApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.widget_size_64);
                    } else {
                        toast.setText(msg);
                    }
                    toast.setGravity(Gravity.BOTTOM, 0, intY == 0 ? 200 : intY);
                    toast.show();
                }
            });
        }
    }

    /**
     * 提示信息 黑色背景 下方显示
     *
     * @param
     */
    public static void showBottomtoast(final int resId) {
        if (resId != 0) {
            if (handler == null) {
                handler = new Handler(Looper.getMainLooper());
            }
            handler.post(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    if (toast == null) {
                        toast = Toast.makeText(BaseApplication.getInstance(), resId, Toast.LENGTH_SHORT);
                        intY = BaseApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.widget_size_64);
                    } else {
                        toast.setText(resId);
                    }

                    toast.setGravity(Gravity.BOTTOM, 0, intY == 0 ? 200 : intY);
                    toast.show();
                }
            });
        }
    }

    /**
     * 提示信息 黑色背景 中间显示
     *
     * @param
     */
    public static void showCentertoast(final int resId) {
        if (resId != 0) {
            if (handler == null) {
                handler = new Handler(Looper.getMainLooper());
            }
            handler.post(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    if (toast == null) {
                        toast = Toast.makeText(BaseApplication.getInstance(), resId, Toast.LENGTH_SHORT);
                        intY = BaseApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.widget_size_64);
                    } else {
                        toast.setText(resId);
                    }
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            });

        }
    }

    /**
     * 提示信息 黑色背景 中间显示
     *
     * @param msg
     */
    public static void showCentertoast(final String msg) {
        showCentertoast(msg, Toast.LENGTH_SHORT);
    }

    public static void showCentertoast(final String msg, final int duration) {
        if (!TextUtils.isEmpty(msg)) {
            if (handler == null) {
                handler = new Handler(Looper.getMainLooper());
            }
            handler.post(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    if (toast == null) {
                        toast = Toast.makeText(BaseApplication.getInstance(), msg, duration);
                        intY = BaseApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.widget_size_64);
                    } else {
                        toast.setText(msg);
                    }
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            });

        }
    }
}
